package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.UserBanquetJoin;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBanquetJoinDaoImpl extends TempDataObjectDao<UserBanquetJoin> {

    //获取30天的记录
    public Map<Long, List<UserBanquetJoin>> getUserBanquetJoinMap() {
        long startTime = System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000;
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_banquet_join where joinTime >= ?";
        Map<Long, List<UserBanquetJoin>> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, startTime);

            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetJoin data = parse(rs);
                if(!res.containsKey(data.getUserId1())){
                    res.put(data.getUserId1(),new ArrayList<>());
                }
                res.get(data.getUserId1()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }


    @Override
    protected boolean addData(UserBanquetJoin data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_banquet_join(userId1,userId2,joinTime) values(?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId1());
            call.setLong(i++, data.getUserId2());
            call.setLong(i++, data.getJoinTime());

            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    /*@Override
    protected boolean updateData(UserBanquetRelationship data) {
        PreparedStatement call = null;
        String sqlUser1ToUser2 = "UPDATE t_u_banquet_join SET user1ToUser2=?,userBaseInfo1=?,updateTime=? where userId1=? and userId2";
        String sqlUser2ToUser1 = "UPDATE t_u_banquet_join SET user2ToUser1=?,userBaseInfo2=?,updateTime=? where userId1=? and userId2";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            int i = 1;
            if(data.getUserId1() == data.getPlayerId()){
                call = getDb().getConnection().prepareStatement(sqlUser1ToUser2);
                call.setInt(i++, data.getUser1ToUser2());
                call.setString(i++, data.getUserBaseInfo1JsonStr());
            }else{
                call = getDb().getConnection().prepareStatement(sqlUser2ToUser1);
                call.setInt(i++, data.getUser2ToUser1());
                call.setString(i++, data.getUserBaseInfo2JsonStr());
            }
            call.setLong(i++, data.getUserId1());
            call.setLong(i++, data.getUserId2());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }*/

    @Override
    protected boolean updateData(UserBanquetJoin data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_banquet_join SET joinTime=? where userId1=? and userId2=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getJoinTime());
            call.setLong(i++, data.getUserId1());
            call.setLong(i++, data.getUserId2());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBanquetJoin parseData(ResultSet rs) throws SQLException {
        UserBanquetJoin data = new UserBanquetJoin();
        data.setUserId1(rs.getLong("userId1"));
        data.setUserId2(rs.getLong("userId2"));
        data.setJoinTime(rs.getLong("joinTime"));
        return data;
    }

}
